# FROM nvcr.io/nvidia/deepstream:5.0-20.07-triton
FROM registry.cn-beijing.aliyuncs.com/xxxt/deepstream:5.0-20.07-triton

RUN sed -i s/archive.ubuntu.com/mirrors.aliyun.com/g /etc/apt/sources.list && sed -i s/security.ubuntu.com/mirrors.aliyun.com/g /etc/apt/sources.list && apt-get update

RUN apt-get update && apt install --no-install-recommends -y \
    ca-certificates \
    python-gst-1.0 \
    wget

# allow GObject to find typelibs
ENV GI_TYPELIB_PATH /usr/lib/x86_64-linux-gnu/girepository-1.0/

# use conda to simplify some dependency managemeny
RUN wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh && \
    /bin/bash ~/miniconda.sh -b -p /opt/conda && \
    rm ~/miniconda.sh && \
    ln -s /opt/conda/etc/profile.d/conda.sh /etc/profile.d/conda.sh

ENV PATH /opt/conda/bin:${PATH}
ENV LD_LIBRARY_PATH /usr/local/cuda-10.2/compat:/opt/conda/lib:/opt/nvidia/deepstream/deepstream-5.0/lib:${LD_LIBRARY_PATH}

RUN conda install -y python=3.7
RUN conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
RUN conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
RUN conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
RUN conda config --set show_channel_urls yes
# RUN conda config --set allow_conda_downgrades true
# RUN conda install conda=4.6.11
RUN conda install -y cudatoolkit=10.2 pytorch torchvision

RUN conda install -y -c conda-forge \
    pygobject \
    scikit-image

# Nvidia Apex for mixed-precision inference
RUN git clone https://gitee.com/leonexu/apex /build/apex
WORKDIR /build/apex
RUN pip install -v --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" .

RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
RUN pip install --upgrade cython
RUN pip install --upgrade gil_load

# Gstreamer debug output location
env GST_DEBUG_DUMP_DOT_DIR=/app/logs

RUN git clone https://gitee.com/leonexu/pytorch-video-pipeline 
RUN mkdir ~/.cache/torch/hub -p
RUN mv pytorch-video-pipeline/NVIDIA_DeepLearningExamples_torchhub ~/.cache/torch/hub/
RUN python -c "import torch; torch.hub.load('NVIDIA/DeepLearningExamples:torchhub', 'nvidia_ssd', model_math='fp32')"  | :
	# wget https://github.com/NVIDIA/DeepLearningExamples/archive/torchhub.zip /home/xling/.cache/torch/hub/torchhub.zip
RUN python -c "import torch; torch.hub.load('NVIDIA/DeepLearningExamples:torchhub', 'nvidia_ssd', model_math='fp16')" | :

RUN rm -rf /var/lib/apt/lists/* && \
    conda clean -afy

WORKDIR /app
